package conf

import (
	"errors"
	"io/ioutil"
	"multimedia/logger"

	log "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v3"
)

var ConfigFileName string

var ConfigFile Config

func init() {
	err := LoadConfig("")
	if err != nil {
		return
	}
	err = logger.InitLogger(&ConfigFile.Log)
	if err != nil {
		log.Errorf("InitLogger error %v", err)
		return
	}
	log.Infof("InitLogger finished :[%v],[%v]", ConfigFileName, ConfigFile)
}

// Config 配置文件结构
type Config struct {
	MediaHost        string
	CallbackHost     string
	ConfeNoMakerHost string
	SdkAppId         int64
	Key              string
	MysqlCon         string
	RedisCon         string
	PingPeriod       int
	Secret           string
	Template         string
	FSConf           FSConfig
	Log              logger.Loginfo
	SmsConf          SmsConfig
	RecordPath       string
}
type SmsConfig struct {
	UserName string
	ApiKey   string
}
type FSConfig struct {
	FSAddr     string
	FSPassWord string
}

// LoadConfig ...
func LoadConfig(filename string) error {
	if len(filename) == 0 {
		ConfigFileName = "./config.yml"
	} else {
		ConfigFileName = filename
	}

	file, err := ioutil.ReadFile(ConfigFileName)
	if err != nil {
		log.Errorf("加载 config.yml 出错")
		return err
	}

	if len(file) == 0 {
		log.Errorf("未发现 config.yml文件")
		return errors.New("未发现 config.yml文件")
	}
	err = yaml.Unmarshal(file, &ConfigFile)
	if err != nil {
		log.Errorf("error: %v", err)
		return err
	}
	return nil
}
